package com.cneiu.ppms.web.interceptor;

import com.cneiu.ppms.component.Success;
import com.cneiu.ppms.utils.StringUtils;
import com.cneiu.ppms.web.model.UserModel;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;

/**
 * 后天权限拦截器
 * 
 * @author john
 * @date 2013-03-27
 */
public class SysInterceptor implements Interceptor {
	
	public void intercept(ActionInvocation ai) {
		Controller controller = ai.getController();
		UserModel users = controller.getSessionAttr("sysuser");
		if (users != null) {
			ai.invoke();
		} else {
			String key = ai.getActionKey();
			String[] notIntercept = {"/users/user/loginVerify","/users/user/login"};
			boolean filter = true;
			for(String inter:notIntercept){
				if(key.equals(inter)){
					filter = false;
				}
			}
			if(filter){
				Success obj = new Success();
				obj.setSuccess(false);
				obj.setMessage("你还没有登录或者登录信息过期,请登录后再操作!");
		        String callback = null;
				if (controller.getPara("callback") != null) {
					callback = controller.getPara("callback");
				}
				String Json = StringUtils.renderAjax(callback, obj);
				controller.renderText(Json);
			}else{
				ai.invoke();
			}
		}
	}
	
}
